import React, {useEffect, useState} from "react";
import {Button, Input, message, Modal, Pagination, Space, Table, TableProps, Tag} from "antd";
import InterviewVo = API.InterviewVo;
import {page} from "@/services/swagger/interviewController";
import {history, useModel} from "@umijs/max";

const InterviewManage: React.FC = () => {
  const columns: TableProps<API.InterviewVo>['columns'] = [
    {
      title: '面试id',
      dataIndex: 'id',
      key: 'id',
      render: (text) => <a onClick={() => history.push(`/interview/result/${text}`)}>{text}</a>,
    },
    {
      title: '用户id',
      dataIndex: 'userId',
      key: 'userId'
    },
    {
      title: '岗位名称',
      dataIndex: 'jobTitle',
      key: 'jobTitle',
    },
    {
      title: '岗位职责',
      dataIndex: 'jobDescription',
      key: 'jobDescription',
    },
    {
      title: '简历内容',
      dataIndex: 'resumeContent',
      key: 'resumeContent',
      render: (_, {resumeContent}) => {
        let startContent = resumeContent?.substring(0, 8);
        return <a >{startContent}</a>
      }
    },
    {
      title: '面试状态',
      key: 'tags',
      dataIndex: 'interviewStatus',
      render: (_, { interviewStatus }) => {
        let color = 'green';
        let status = '进行中'
        if (interviewStatus === 0) color = 'red'; status= '未开始';
        if (interviewStatus === 1) color = 'blue'; status='进行中'
        if (interviewStatus === 2) color = 'green'; status='已完成';
        return <Tag color={color}>{status}</Tag>
      }
    },
    {
      title: '创建时间',
      key: 'createTime',
      dataIndex: 'createTime'
    },
    {
      title: '修改时间',
      key: 'updateTime',
      dataIndex: 'updateTime'
    },
  ];

  const [interviewVoList, setInterviewVoList] = useState<API.InterviewVo[] | undefined>([])
  const [current, setCurrent] = useState(1)
  const [total, setTotal] = useState<number | undefined>(0)
  const [pageSize, setPageSize] = useState(10)
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};
  const [interviewQueryRequest, setInterviewQueryRequest] = useState<API.InterviewQueryRequest>({
    current: current || 1,
    pageSize: pageSize || 10,
    userId: currentUser.id
  })


  /**
   * 获取面试列表
   */
  const getInterviewList = async () => {
    const data = await page(interviewQueryRequest);
    if (data.code === 0){
      message.success("获取面试列表成功")
      setInterviewVoList(data.data);
      setTotal(interviewVoList?.length)
    }else {
      message.error("获取面试列表失败： " + data.message);
      setInterviewVoList([])
      setTotal(0)
    }
  }
  /**
   * 处理分页变化
   */
  const changePage = (currentPage: number, currentPageSize: number) => {
    setCurrent(currentPage);
    setPageSize(currentPageSize);
  }

  // 进入页面就获取用户列表
  useEffect(() => {
    getInterviewList().then(r => {})
  },[current, pageSize])
  return (
    <div>
      <h1>面试管理</h1>
      <Table<InterviewVo> columns={columns} dataSource={interviewVoList} rowKey="id" pagination={false}/>
      <Pagination style={{ marginTop: 16, textAlign: 'right' }} current={current} pageSize={pageSize} total={total} onChange={changePage} showSizeChanger />
    </div>
  );
}

export default InterviewManage;
